package com.demohomework.homeworkdemo.service.impl;

import com.demohomework.homeworkdemo.mapper.TeacherMapper;
import com.demohomework.homeworkdemo.pojo.Teacher;
import com.demohomework.homeworkdemo.service.TeacherService;
import com.demohomework.homeworkdemo.util.Md5Util;
import com.demohomework.homeworkdemo.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Teacher findbyUsername(String username) {
        Teacher t = teacherMapper.findbyUsername(username);
        return t;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5Password  = Md5Util.string2MD5(password);
        //添加
        teacherMapper.add(username, md5Password);
    }

    @Override
    public void update(Teacher teacher) {
        teacherMapper.update(teacher);
    }

    @Override
    public void updatePwd(String newPwd) {
        //加密
        String md5Password  = Md5Util.string2MD5(newPwd);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        //修改
        teacherMapper.updatePwd(md5Password,id);
    }

    @Override
    public List<Teacher> list() {
        return teacherMapper.list();
    }

    @Override
    public void delete(Integer id) {
        teacherMapper.delete(id);
    }

    @Override
    public void add(Teacher teacher) {
        String defaultPassword = "123456";
        String md5Password  = Md5Util.string2MD5(defaultPassword);
        teacher.setPassword(md5Password);
        teacherMapper.teaAdd(teacher);
    }

    @Override
    public Teacher get(Integer id) {
        return teacherMapper.get(id);
    }
}
